<?php

namespace app\index\controller;
 

/**
 * 财务管理
 *
 * @icon fa fa-circle-o
 */
class Finance extends User
{
    
    /**
     * FinanceLog模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Finance'); 
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    public function index()
    {  
        return;
    }
    public function finance_list($log_time='')
    { 

        $params = $this->request->param(); 
        $site_id = empty($params['site_id'])?1:$params['site_id']; 
        $building = model('Site')->where('id',$site_id)->find()->toArray();
        $this->view->assign("building_name", $building['building_name']);
        $where['site_id'] = $site_id;
        $where['user_id'] = $this->auth->id;
        // list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $sort = 'id';
        $order = 'desc';
        $offset = 0;
        $limit = 100;
        $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

        $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

        $list = collection($list)->toArray();
        $result = array("total" => $total, "rows" => $list);
        $this->view->assign("result", $result);  

        $this->view->assign("zcsum", $this->model->where($where)->where(['apply_type'=>2])->sum('amount')); 
        $this->view->assign("srsum", $this->model->where($where)->where(['apply_type'=>1])->sum('amount')); 
        $list = model('Site')->where(["building_delete"=>0])->select(); 
        if($list){ 
            foreach ($list as $key => $value) {
                $siteList[$key]['value'] = $value['id']; 
                $siteList[$key]['text'] = $value['building_name'];
            } 
            $this->view->assign("siteList",json_encode($siteList));
        }else{
            $this->error('暂无工地数据');
        }

        return $this->view->fetch();
    }
    public function finance_detail($id){ 
        $row = $this->model->get($id)->toArray();
        $this->view->assign("result", $row);  
        return $this->view->fetch();
    }
    public function finance_add()
    {  
        if ($this->request->isPost())
        {
            $params = $this->request->post();
            try { 
                $params['user_id'] = $this->auth->id;
                $params['user_name'] = $this->auth->username;
                $params['organization_id'] = $this->auth->organization_id;
                $params['apply_time'] = time(); 
                $result = $this->model->save($params);
                if ($result !== false)
                {
                    $this->success();
                }
                else
                {
                    $this->error($this->model->getError());
                }
            } catch (Exception $e) {
                $this->error($e->getMessage());
            } 
        } 
        $list = model('Site')->where(["building_delete"=>0])->select(); 
        if($list){ 
            foreach ($list as $key => $value) {
                $siteList[$key]['value'] = $value['id']; 
                $siteList[$key]['text'] = $value['building_name'];
            } 
            $this->view->assign("siteList",json_encode($siteList));
        }else{
            $this->error('暂无工地数据');
        }
        return $this->view->fetch();
    } 
 
}
